package com.dragon.utilslibrary.tools;

import android.content.Context;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Calendar;
import java.util.Locale;

public class ConvertUtils {
	public static byte[] hexStringToByteArray(String s) {
	    int len = s.length();
	    byte[] data = new byte[len / 2];
	    for (int i = 0; i < len; i += 2) {
	        data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
	                             + Character.digit(s.charAt(i+1), 16));
	    }
	    
	    return data;
	}
	/**
	 * 数组转换成十六进制字符串
	 * @param byteArray
	 * @return
	 */
	public static String byteArrayTohexString(byte[] byteArray) {
		StringBuilder sb = new StringBuilder(byteArray.length * 2);
		for (byte b : byteArray) {
			if ((b & 0xff) < 0x10) {
				sb.append("0");
			}
			sb.append(Integer.toHexString(0xff & b));
		}
		
		return sb.toString().toLowerCase();
	}
	
	
	/**
	 * 数组转换成十六进制字符串
	 * @param b
	 * @return
	 */
    public static String byte2hex(byte[] b) {
  
        String hs = "";
        String stmp = "";
        for (int n = 0; n < b.length; n++) {  
            stmp = (Integer.toHexString(b[n] & 0XFF));
            if (stmp.length() == 1)  
                hs = hs + "0" + stmp;  
            else  
                hs = hs + stmp;  
            if (n < b.length - 1)  
                hs = hs + ":";  
        }  
        return hs.toUpperCase();  
  
    }

    /**
     * 16进制字符转ascii字符
     * @param s
     * @return
     */
	public static String toStringHex1(String s) {
		byte[] baKeyword = new byte[s.length() / 2];
		for (int i = 0; i < baKeyword.length; i++) {
			try {
				baKeyword[i] = (byte) (0xff & Integer.parseInt(s.substring(
						i * 2, i * 2 + 2), 16));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		try {
			s = new String(baKeyword, "ASCII");
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		return s;
	}

    /**
     * byte数组转ascii字符
     * @param baKeyword
     * @return
     */
    public static String toStringHex1(byte[] baKeyword) {
        String s = "";
        try {
            s = new String(baKeyword, "ASCII");
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        return s;
    }

	public static byte[] byte2ArraySet(byte[] byte1, byte[] byte2) {
		byte[] res_byte = new byte[byte1.length + byte2.length];
		System.arraycopy(byte1, 0, res_byte, 0, byte1.length);
		System.arraycopy(byte2, 0, res_byte, byte1.length, byte2.length);
		return res_byte;
	}

	public static byte[] byte3ArraySet(byte[] byte1, byte[] byte2, byte[] by3) {
		byte[] res_byte = new byte[byte1.length + byte2.length + by3.length];
		System.arraycopy(byte1, 0, res_byte, 0, byte1.length);
		System.arraycopy(byte2, 0, res_byte, byte1.length, byte2.length);
		System.arraycopy(by3, 0, res_byte, byte1.length + byte2.length, by3.length);
		return res_byte;
	}

	public static byte[] byte4ArraySet(byte[] byte1, byte[] byte2, byte[] by3, byte[] by4) {
		byte[] res_byte = new byte[byte1.length + byte2.length + by3.length + by4.length];
		System.arraycopy(byte1, 0, res_byte, 0, byte1.length);
		System.arraycopy(byte2, 0, res_byte, byte1.length, byte2.length);
		System.arraycopy(by3, 0, res_byte, byte1.length + byte2.length, by3.length);
		System.arraycopy(by4, 0, res_byte, byte1.length + byte2.length + by3.length, by4.length);
		return res_byte;
	}

	public static byte[] byte5ArraySet(byte[] byte1, byte[] byte2, byte[] by3, byte[] by4, byte[] by5) {
		byte[] res_byte = new byte[byte1.length + byte2.length + by3.length + by4.length + by5.length];
		System.arraycopy(byte1, 0, res_byte, 0, byte1.length);
		System.arraycopy(byte2, 0, res_byte, byte1.length, byte2.length);
		System.arraycopy(by3, 0, res_byte, byte1.length + byte2.length, by3.length);
		System.arraycopy(by4, 0, res_byte, byte1.length + byte2.length + by3.length, by4.length);
		System.arraycopy(by5, 0, res_byte, byte1.length + byte2.length + by3.length + by4.length, by5.length);
		return res_byte;
	}




	/**
	 * 十六进制字符转化成整形
	 * @param ox
	 * @return
	 * @throws Exception
	 */
	public static int OxStringtoInt(String ox) throws Exception
	{
		ox = ox.toLowerCase();
		if (ox.startsWith("0x"))
		{
			ox = ox.substring(2, ox.length());
		}
		int ri = 0;
		int oxlen = ox.length();
		if (oxlen > 8)
			throw (new Exception("too lang"));
		for (int i = 0; i < oxlen; i++)
		{
			char c = ox.charAt(i);
			int h;
			if (('0' <= c && c <= '9'))
			{
				h = c - 48;
			}
			else if (('a' <= c && c <= 'f'))
			{
				h = c - 87;

			}
			else if ('A' <= c && c <= 'F')
			{
				h = c - 55;
			}
			else
			{
				throw (new Exception("not a integer "));
			}
			byte left = (byte) ((oxlen - i - 1) * 4);
			ri |= (h << left);
		}
		return ri;

	}
	
	/**
	 * 将时间String的hour,minute转化成Int值
	 * @param time
	 * @return
	 */
	public static int stringTimeToIntTime(String time)
	{
		try
		{
			String[] times = time.split(":");
			int intTime = Integer.parseInt(times[0]) * 60 + Integer.parseInt(times[1]);
			return intTime;
		}
		catch(Exception e)
		{
			LogUtils.i("Utils", "e: " + e);
		}
		return 0;
	}
	
	/**
	 * 根据手机分辨率从dp转成px
	 * 
	 * @param context
	 * @param dpValue
	 * @return
	 */
	public static int dip2px(Context context, float dpValue)
	{
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (dpValue * scale + 0.5f);
	}

	/**
	 * 根据手机的分辨率从 px(像素) 的单位 转成为 dp
	 */
	public static int px2dip(Context context, float pxValue)
	{
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (pxValue / scale + 0.5f) - 15;
	}

	/** 长度单位转换：ftin格式必须为 1'00" 才能进行转换 否则返回 null **/
	public static String ftin2cm(String ft_in)
	{
		if (ft_in == null || ft_in.trim().equals("") || ft_in.length() != 5)
		{
			return null;
		}
		int in;
		if (ft_in.substring(2, 3).equals("0"))
		{
			in = Integer.parseInt(ft_in.substring(0, 1)) * 12 + Integer.parseInt(ft_in.substring(3, 4));
		}
		else
		{
			in = Integer.parseInt(ft_in.substring(0, 1)) * 12 + Integer.parseInt(ft_in.substring(2, 4));
		}
		in = (int) (in * 2.54);
		return in + "";
	}

	/*** 长度单位转换：英制转公制 000 格式转 0'00" **/
	public static String cm2ftin(String cm)
	{
		if (cm == null || cm.trim().equals(""))
		{
			return null;
		}
		int in = (int) new BigDecimal(Integer.parseInt(cm.trim()) / 2.54).setScale(0, BigDecimal.ROUND_HALF_UP)
				.intValue();
		String ftinString = null;
		if (in % 12 < 10)
		{
			ftinString = in / 12 + "\'" + "0" + in % 12 + "\"";
		}
		else
		{
			ftinString = in / 12 + "\'" + in % 12 + "\"";
		}
		return ftinString;
	}

	/*** 重量单位转换：lbs 000 转换为 kg 000 1lbs = 0.45 kg **/
	public static String lbs2kg(String lbs)
	{
		int kgString = (int) new BigDecimal(Integer.parseInt(lbs) * 0.45).setScale(0, BigDecimal.ROUND_HALF_UP)
				.intValue();
		return kgString + "";
	}

	/*** 重量单位转换：kg 000 转换为 lbs 000 **/
	public static String kg2lbs(String kg)
	{
		int lbsString = (int) new BigDecimal(Integer.parseInt(kg) / 0.45).setScale(0, BigDecimal.ROUND_HALF_UP)
				.intValue();
		return lbsString + "";
	}
	
	/**1970-01-01 00:00:00,取年份前四个字符，然后返回年龄*/
	public static String birthdayToage(String birthday){
		String age=null;
		String birthday_Year=birthday.substring(0,4);
		Calendar now= Calendar.getInstance();
		int NowYear=now.get(Calendar.YEAR);
		return Integer.toString(NowYear- Integer.parseInt(birthday_Year));
	}
	/*******摄氏度转华氏度***********/
	public static int CelsiusToFahrenheit(int celsius){
		int fahrenheit = (int)(32+(celsius*9)/5) ;
		return fahrenheit;
	}

	/**
	 * 身高cm转英寸in
	 * @param cm 身高cm
	 * @return
	 */
	public static String cmToin(int cm){
		DecimalFormat dfDecimalFormat = new DecimalFormat("0.00", new DecimalFormatSymbols(Locale.US));
		float in = (float) (cm*0.3937008);
		return CommonUtils.instandPoint(dfDecimalFormat.format(in))+"''";
	}

	/**
	 * 米m转码yd
	 * @param m 米
	 * @return
	 */
	public static String mToYd(int m){
		DecimalFormat dfDecimalFormat = new DecimalFormat("0.0", new DecimalFormatSymbols(Locale.US));
		float yd = (float) (m*1.0936133);
		return dfDecimalFormat.format(yd);
	}

	/**
	 * 千米km转英里mile
	 * @param km
	 * @return
	 */
	public static String kmToMile(float km){
		DecimalFormat dfDecimalFormat = new DecimalFormat("0.00", new DecimalFormatSymbols(Locale.US));
		float mile = (float) (km*0.6213712);
		return dfDecimalFormat.format(mile);
	}

	/**
	 * 千克转磅
	 * @param kg
	 * @return
	 */
	public static String kgToLb(int kg){
		DecimalFormat dfDecimalFormat = new DecimalFormat("0", new DecimalFormatSymbols(Locale.US));
		float lb = (float) (kg*2.2046226);
		return dfDecimalFormat.format(lb);
	}

	public static int numToStep(int num){
		int step = num*100;
		return step;
	}

	public static String numToCal(int num, int weight){
        DecimalFormat dfDecimalFormat = new DecimalFormat("0.0", new DecimalFormatSymbols(Locale.US));
//        int weight = SharedPreferencesUser.getSharedIntData(AppConfig.mContext, SharedPreferencesUser.USER_WEIGHT);
        float cal = CommonUtils.step2Cal(num*100 + "", weight, true)/1000f;
		return dfDecimalFormat.format(cal);
	}

	public static int numToDistance(int num, int height, int sex){
//		int distance = num*80;
//		int height = SharedPreferencesUser.getSharedIntData(AppConfig.mContext, SharedPreferencesUser.USER_HEIGHT);
//		int sex = SharedPreferencesUser.getSharedIntData(AppConfig.mContext, SharedPreferencesUser.USER_SEX);
		int cm = CommonUtils.step2Distance(num*100 + "", height, sex);
		int distance;
		DecimalFormat decimalFormat = new DecimalFormat("0", new DecimalFormatSymbols(Locale.US));
		String format;
		if (cm < 100){
			format = decimalFormat.format(1);
		} else {
			format = decimalFormat.format(cm*1.0/100.0);
		}
		distance = (int) Float.parseFloat(format);
		return distance;
	}

	public static int nunToHourGoal(int num){
		int hour = num;
		return hour;
	}

	/**
	 * 首字母大写
	 * @param name
	 * @return
     */
	public static String captureName(String name) {
		name = name.substring(0, 1).toUpperCase() + name.substring(1);
		return  name;
	}

	/**
	 * 首字母大写
	 * @param name
	 * @return
     */
	public static String captureName2(String name) {
		char[] cs=name.toCharArray();
		cs[0]-=32;
		return String.valueOf(cs);
	}


	/**
	 * 是否为字母
	 * @param s
	 * @return
     */
	public static boolean isChar(String s)
	{
		char   c   =   s.charAt(0);
		int   i   =(int)c;
		if((i>=65&&i<=90)||(i>=97&&i<=122))
		{
			return   true;
		}
		else
		{
			return   false;
		}
	}
}
